Skip to content

feat(risk): add config-driven bucket ladder and merged whole-share sizing - #824

Merged
Pigbibi merged 2 commits into
mainfrom
feat/bucket-capital-ladder
Nov 28, 2026
Merged

Pigbibi merged 2 commits into
mainfrom
feat/bucket-capital-ladder

Conversation

@Pigbibi

@Pigbibi Pigbibi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

AI assistance

This PR was drafted with AI coding assistance and audited via local gpt-6.2-astra read-only review (0 P0; all 6 P1 and 4 P2 findings fixed with regression tests) plus pytest and ruff.

What changed

  • Add src/quant_platform_kit/risk/bucket_capital_ladder.py implementing the offline, config-driven Layer 1 four-slot capital-ladder orchestrator:
    • Half-open capital tiers (BELOW_TIER1, TIER1 [$1.5k, $15k), TIER2 [$15k, $50k), TIER3 >= $50k) with hard $50k Bucket D floor, default A/B/D = 70/30/0 (C = 0% read-only), config-driven per-bucket broker routing, bucket_d_single_stock_max_usd, and console Owner decision prompts.
    • Cross-sleeve duplicate-ticker merging (Mapping or Sequence[tuple[str, float]]) before exact Decimal whole-share flooring, halted-symbol retention (rejecting bare str), passive single-symbol cap breach add-blocking (while permitting explicit trims/exits), incremental-buy-only slippage, and canonical estimate_ibkr_commission_usd fee modeling.
  • Export all symbols in src/quant_platform_kit/risk/__init__.py.
  • Add comprehensive unit tests in tests/test_bucket_capital_ladder.py.

How verified

  • uv run pytest tests/test_bucket_capital_ladder.py tests/test_benchmark_dca_and_regime.py tests/test_twr_capital_ladder.py -q -> all passed.
  • uv run ruff check src/quant_platform_kit/risk/bucket_capital_ladder.py src/quant_platform_kit/risk/__init__.py tests/test_bucket_capital_ladder.py -> All checks passed!.

Why

Deliver the first config-driven building block for the permanent full-lifecycle Layer 1 architecture so tier progression, bucket weights, and broker routing are controlled by configuration rather than repeated strategy code edits (live=OFF, paper=OFF, zero broker I/O).

Risk

  • Risk level: R2 (pure offline library module + unit tests; zero runtime, broker, or live-service side effects).

@Pigbibi Pigbibi added the agent-approved An agent cleared this high-risk change on the current head; not an owner confirmation label Nov 28, 2026
@Pigbibi
Pigbibi enabled auto-merge (squash) November 28, 2026 23:44
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T23:57:25.274617Z 89147d8 Manual request
🔒 Security Review ✅ Completed 2026-11-28T23:58:24.377957Z 89147d8 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 887aed80e0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Pigbibi Pigbibi added agent-approved An agent cleared this high-risk change on the current head; not an owner confirmation and removed agent-approved An agent cleared this high-risk change on the current head; not an owner confirmation labels Nov 28, 2026
@Pigbibi

Pigbibi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 89147d803c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Pigbibi
Pigbibi merged commit ce6369f into main Nov 28, 2026
15 of 18 checks passed
@Pigbibi
Pigbibi deleted the feat/bucket-capital-ladder branch November 28, 2026 23:57
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: 89147d803c

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@qsl-gemini-reviewer

Copy link
Copy Markdown

AI Code Review (gemini / gemini-4-neon)

The new offline Layer 1 capital-ladder and merged whole-share sizing module is clean, self-contained, and preserves key risk boundaries (hard $50k Bucket D floor, half-open tiers, Decimal flooring, and sell-before-buy accounting). However, there are two correctness/robustness bugs (an O(qty) linear decrement loop in buy affordability solving and str(None) bypassing broker-route/symbol validation) plus missing tests for the core passive-breach add-blocking, cap-clamping, and cash-rationing paths.

Reviewed commit: 887aed80e0 · Coverage: complete · Unresolved findings: true

Findings

  • [MEDIUM] src/quant_platform_kit/risk/bucket_capital_ladder.py: In compute_merged_whole_share_orders (lines 431-439), affordability adjustment decrements qty -= 1 linearly in a while qty > 0 loop. Because IBKR fees can be up to 1% of notional, low-priced symbols with large cash balances (e.g., Tier 3 NAV >= $50k and share price <= $0.10) force thousands to millions of iterations. Since notional + fee is strictly increasing in qty, replace the linear loop with a binary search over [0, min(need_q, max_by_px)].
  • [MEDIUM] src/quant_platform_kit/risk/bucket_capital_ladder.py: BucketLadderConfig.__post_init__ (lines 95-100) normalizes broker venues via str(raw).strip().lower() without checking isinstance(raw, str), so broker_routes={'A': None, ...} silently passes validation with venue 'none' instead of failing closed with ValueError (and non-Mapping broker_routes raises AttributeError). Similarly, _normalize_unique_mapping and _normalize_allocations convert None keys via str(...) into symbol 'NONE' instead of rejecting non-string symbols.
  • [MEDIUM] tests/test_bucket_capital_ladder.py: Critical risk-guard paths lack test coverage: test_merged_whole_share_orders_halts_passive_breach_exit_slippage_and_decimal_precision has a comment claiming to test 'Passive breach blocks adds when target > held', but only passes target=0.0 (full exit) and never tests target > held_val (verifying adds are blocked while held shares are retained without forced sell-down) or active cap clamping (held <= cap < target). Cash-constrained buy rationing (descending dollar-shortfall priority) and zero_share_target_symbols are also untested.
  • [LOW] src/quant_platform_kit/risk/bucket_capital_ladder.py: In _compute_desired_shares (lines 333-336), symbols in passive breach are appended to passive_breach_blocked_adds unconditionally even when base_tgt_q <= held_q (an explicit trim or full exit where no add was blocked). Either restrict appending to base_tgt_q > held_q or rename the field to reflect all passively breached symbols.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-approved An agent cleared this high-risk change on the current head; not an owner confirmation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant